﻿using System;
using System.Collections.Generic;
using System.Text;
using BlueLotus7.MetaWeblogUtil.CnBlogs.Contract;
using BlueLotus7.MetaWeblogUtil.CnBlogs.Contract.Interfaces;
using BlueLotus7.MetaWeblogUtil.Contract;
using BlueLotus7.MetaWeblogUtil.Contract.Interfaces;
using CookComputing.XmlRpc;

namespace BlueLotus7.MetaWeblogUtil.CnBlogs
{
    public class CnBlogsAgent : BaseAgent<ICnBlogsMetaWeblogProxy>, IMetaWeblogAgent<ICnBlogsMetaWeblogProxy>
    {
        protected override string XmlRpcUrlFormat
        {
            get
            {
                return "http://www.cnblogs.com/{0}/services/metaweblog.aspx";
            }
        }

        public CnBlogsAgent(string loginName, string password, string xmlRpcUrlIdentity)
            : base(string.Empty, loginName, password, xmlRpcUrlIdentity)
        {
        }

        #region Methods

        protected override void Initialize()
        {
            base.Initialize();
            if (!string.IsNullOrEmpty(XmlRpcUrlFormat))
            {
                this.Proxy.Url = string.Format(XmlRpcUrlFormat, this.XmlRpcUrlIdentity);
            }
        }

        public string AddPost(CnBlogsPost post)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_newPost;
            return Proxy.NewPost(string.Empty, this.LoginName, this.Password, post, true);
        }

        public void DeletePost(string postId)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.blogger_deletePost;
            Proxy.DeletePost(this.AppKey, postId, this.LoginName, this.Password, true);
        }

        public void UpdatePost(CnBlogsPost post)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_editPost;
            Proxy.EditPost(post.postid.ToString(), this.LoginName, this.Password, post, true);
        }

        public CnBlogsPost GetPost(string postId)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_getPost;
            return Proxy.GetPost(postId, this.LoginName, this.Password);
        }

        public CnBlogsPost[] GetRecentPosts(int noOfPosts)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_getRecentPosts;
            return Proxy.GetRecentPosts(string.Empty, this.LoginName, this.Password, noOfPosts);
        }

        public BlogInfo[] GetUserBlogs()
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.blogger_getUsersBlogs;
            return Proxy.GetUserBlogs(this.AppKey, this.LoginName, this.Password);
        }

        public CategoryInfo[] GetCategories()
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_getCategories;
            return Proxy.GetCategories(string.Empty, this.LoginName, this.Password);
        }

        public UrlData NewMediaObject(string blogid, FileData mediaObject)
        {
            Proxy.XmlRpcMethod = XmlRpcMethodList.metaWeblog_newMediaObject;
            return Proxy.NewMediaObject(blogid, this.LoginName, this.Password, mediaObject);
        }

        #endregion
    }
}
